<template>
    <div class="app-container">
        <!-- 功能性按钮 -->
        <el-form :inline="true" :model="query" size="mini">
            <el-form-item label="菜单名称">
                <el-input v-model.trim="query.name"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button icon="el-icon-search" type="primary" @click="fetchData">查询</el-button>
                <el-button  icon="el-icon-refresh" @click="reload">重置</el-button>
                <el-button  icon="el-icon-plus" type="primary"  @click="handleAdd(0)"> 新增</el-button>
            </el-form-item>
        </el-form>
        <!-- 表格主体  row-key="id"要指定唯一标识的属性名，其中数据要包含children才会被渲染为树状-->
        <el-table :data="list"
                  stripe 
                  style="width: 100%"
                   row-key="id">
            <el-table-column  align="center" label="序号" width="60" type="index"></el-table-column>
            <el-table-column  prop="name" align="center" label="名称"></el-table-column>
            <el-table-column prop="url" label="请求地址"></el-table-column>
            <el-table-column prop="code" label="权限标识"></el-table-column>
            <el-table-column prop="type" label="类型" align="center">
                <template slot-scope="scope">
                    <span v-if="scope.row.type === 1">目录</span>
                     <span v-if="scope.row.type === 2">菜单</span>
                      <span v-if="scope.row.type === 3">按钮</span>
                </template>
            </el-table-column>
            <el-table-column prop="icon" label="图标" align="center">
                <template slot-scope="scope">
                    <i :class="scope.row.icon"></i>
                </template>
            </el-table-column>
            <el-table-column prop="sort" label="排序" align="center"></el-table-column>
            <el-table-column  label="操作" width="266px" >
                 <template slot-scope="scope">
                   <el-button type="primary" @click="handleAdd(scope.row.id)" size="mini"><i class="el-icon-circle-plus-outline"></i>新增</el-button>
                    <el-button type="success" @click="handleEdit(scope.row.id)" size="mini">编辑</el-button>
                    <el-button type="danger"  @click="handleDelete(scope.row.id)" size="mini">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 新增或者编辑组件 -->
        <edit :title="edit.title" 
              :visible="edit.visible" 
              :formData="edit.formData"
              :remoteClose="remoteClose"
              >
        </edit>
    </div>
</template>

<script>
import api from "@/api/menu"
import Edit from "./edit"
export default {
    name:"Menu",
    data(){
        return{
            query:{}, //查询列表
            list:[], //列表数据
            edit:{
                title:"",
                visible:false,
                formData:{}
            }
        }
    },
    components:{
        Edit,
    },
    created(){
        this.fetchData()
    },
    methods:{
        async fetchData(){
          const res = await api.getList(this.query)
          this.list = res.data
        },
        //新增菜单,id作为新菜单的parentId 
        handleAdd(id){
            //如果接受的id是0，是在条件查询 的地方点击的，是新增一级菜单，否则是新增某菜单下的子菜单
            this.edit.formData.parentId = id
            //弹出的标题
            this.edit.title = "新增",
            this.edit.visible = true
        },
        //关闭窗口弹窗
        remoteClose(){
             this.edit.formData = {}
             this.edit.visible = false
        },
        //编辑菜单
        async handleEdit(id){
            const res = await api.getById(id)
            if(res.code === 20000){
                this.edit.formData = res.data
                //设置标题
                this.edit.title = "编辑"
                this.edit.visible = true
            }
        },
        //删除菜单
        async handleDelete(id){           
            this.$confirm('确认删除菜单吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
            }).then(() => {
                //发送删除请求  
                api.deleteById(id).then(res=>{
                    //处理响应结果
                    this.$message({
                        type: res.code === 20000 ? "success":"error",
                        message: res.message
                    });
                  //刷新列表
                  this.fetchData()                   
                })          
            }).catch(() => {
                //取消删除
            });
        },
        reload(){
            this.query = {}
            this.fetchData()
        }
    }
}

</script>

<style scoped>

</style>
